package com.tarena.lbs.coupon.dao.repository.impl;

import com.tarena.lbs.common.utils.SnowflakeIdGenerator;
import com.tarena.lbs.coupon.dao.mapper.CouponCodeMapper;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.base.enums.CouponStatusEnum;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponCodePO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author ZHC
 */
@Repository
@Slf4j
public class CouponCodeRepositoryImpl implements CouponCodeRepository {
    @Autowired
    private CouponCodeMapper couponCodeMapper;
    @Autowired
    private SnowflakeIdGenerator idGenerator;

    @Override
    public void insertCouponCode(Integer couponId, CouponParam couponParam) {
        // 新增优惠券code
        log.info("CouponCode数据层:新增优惠券code:{}",couponParam);
        CouponCodePO couponCodePO = new CouponCodePO();
        couponCodePO.setCouponId(couponId);
        // 雪花算法生成优惠券code
        String couponCode = idGenerator.nextId()+"";
        couponCodePO.setCouponCode(couponCode);
        couponCodePO.setStatus(CouponStatusEnum.COUPON_CODE_STATUS_UNALLOCATED.getCode());
        couponCodePO.setBusinessId(couponParam.getBusinessId());
        Date date = new Date();
        couponCodePO.setUpdateAt(date);
        couponCodePO.setCreateAt(date);
        couponCodeMapper.insert(couponCodePO);
    }
}
